from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.models import User
from dhm.models.Warehousing_System.administrator.administrator import Administrator

class RegisterView(APIView):
    def post(self, request):
        data = request.POST
        professional_title = data.get('professional_title', '')
        creation_time = data.get('creation_time', '')
        update_time = data.get('update_time', '')
        username = data.get('username', '').strip()
        password = data.get('password', '').strip()
        phone_number = data.get('phone_number').strip()
        verification_code = data.get('verification_code', '')

        if not username:
            return Response({
                'result': "用户名是必填信息"
            })
        if not professional_title:
            return Response({
                'result': "请选择用户职称"
            })
        if User.objects.filter(username=username).exists():
            return Response({
                'result': "用户名已存在"
            })

        user = User(username=username)
        user.set_password(password)
        user.save()
        Administrator.objects.create(user=user, photo="https://img2.woyaogexing.com/2021/07/12/54ee85611756438db5039649ff837ec1%21400x400.jpeg", phone_number=phone_number, username=username, professional_title=professional_title, creation_time=creation_time, update_time=update_time, verification_code=verification_code)
        return Response({
            'result': "success",
        })
